//
//  CmgeBaseView.m
//  CmgeIosClient
//
//  Created by Gang.Liu on 14-3-21.
//  Copyright (c) 2014年 zhouqing. All rights reserved.
//

#import "LGBaseView.h"

@interface UIView (Traversal)

-(void)traversal:(UIInterfaceOrientation)orientation;

@end

@implementation LGBaseView

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setup];
}

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setup];
    }
    return self;
}

-(void)setup
{
    if ([self respondsToSelector:@selector(setupViews)])
    {
        [self setupViews];
    }
    
    if ([self respondsToSelector:@selector(setupData)])
    {
        [self setupData];
    }
    
    self.orientation = [[UIApplication sharedApplication] statusBarOrientation];
}

-(void)setOrientation:(UIInterfaceOrientation)orientation
{
    _orientation = orientation;
    
    [self traversal:orientation];
    
    [self setNeedsLayout];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    switch (self.orientation) {
		case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            
		case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
        {
            if ([self respondsToSelector:@selector(configViewPortrait)])
            {
                [self configViewPortrait];
            }
            if ([self respondsToSelector:@selector(layoutPortrait)])
            {
                [self layoutPortrait];
            }
        }
            break;
//		case UIInterfaceOrientationLandscapeLeft:
//        case UIInterfaceOrientationLandscapeRight:
//        {
//            if ([self respondsToSelector:@selector(configViewLandscape)])
//            {
//                [self configViewLandscape];
//            }
//            if ([self respondsToSelector:@selector(layoutLandscape)])
//            {
//                [self layoutLandscape];
//            }
//        }
//            break;
    }
    
}

@end

@implementation UIView (Traversal)

-(void)traversal:(UIInterfaceOrientation)orientation
{
    [self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[LGBaseView class]])
        {
            LGBaseView * tempView = (LGBaseView *)obj;
            tempView.orientation = orientation;
        }
        else
        {
            [obj traversal:orientation];
        }
    }];
}

@end

